package com.cat.model.web.action.admin.system;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.alonew.core.commons.page.AjaxObject;
import com.alonew.core.commons.page.Page;
import com.alonew.core.commons.util.CommonUtil;
import com.cat.model.biz.logic.SystemLogic;
import com.cat.model.biz.service.admin.SystemService;
import com.cat.model.common.model.admin.AdminUser;
import com.cat.model.dao.common.PlmCommDAO;
import com.cat.model.web.action.base.AdminAction;

public class AdminManagerAction extends AdminAction {
	
	@Autowired
	private PlmCommDAO plmCommDAO;
	
	@Autowired
	private SystemService systemService;
	
	@Autowired
	private SystemLogic systemLogic;
	
	private static final long serialVersionUID = 1L;
	
	private Integer id;
	private AdminUser adminUser;
	private String newPassword;
	private String prePassword;
	private String password;
	private String name;

	public String list() {
		Map<String, Object> condition = new HashMap<String, Object>();
		Page page = this.fetchPageParams();
		page.setNumPerPage(20);
		
		Long totalCount = plmCommDAO.searchSize("manager.searchAdminUserSize", condition);
		page.setTotalCount(totalCount);
		int start = (page.getPageNum() -1) * page.getNumPerPage();
		if(start < 0){
			start = 0;
		}
		
		@SuppressWarnings("unchecked")
		List<AdminUser> list = (List<AdminUser>)plmCommDAO.search("manager.searchAdminUser", condition, start, page.getNumPerPage());
		
		request.setAttribute("list", list);
		request.setAttribute("size", totalCount);
		return "list";
	}
	
	public String deleteSingle() {
		try {
			systemLogic.deleteAdminUser(id);
		} catch (Exception e) {
			e.printStackTrace();
			this.print(AjaxObject.newError(e.getMessage()).setCallbackType("").toString());
		}
		this.print(AjaxObject.newOk("删除成功！").setCallbackType("").toString());
		return "list";
	}
	
	public String toAdd(){
		return "add";
	}
	
	public String add(){
		try { 
			if(!adminUser.getPassword().equals(newPassword)){
				this.printHTML("<script>parent.callback(" + false + ", '"+ "两次新密码输入不一样！"+"')</script>");
				return "list";
			}
			systemService.addAdminUser(adminUser);
		} catch (Exception e) {
			e.printStackTrace();
			this.printHTML("<script>parent.callback(" + false + ", '"+e.getMessage()+"')</script>");
			return "list";
		}
		this.printHTML("<script>parent.callback(" + true + ", '保存成功！')</script>");
		return "list";
	}
	
	public String toEdit(){
		adminUser = systemLogic.getAdminUser(id);
		return "edit";
	}
	
	public String edit(){
		try { 
			if(!adminUser.getPassword().equals(newPassword)){
				this.printHTML("<script>parent.callback(" + false + ", '"+ "两次新密码输入不一样！"+"')</script>");
				return "list";
			}
			systemService.updateAdminUser(adminUser);
		} catch (Exception e) {
			e.printStackTrace();
			this.printHTML("<script>parent.callback(" + false + ", '"+e.getMessage()+"')</script>");
			return "list";
		}
		this.printHTML("<script>parent.callback(" + true + ", '保存成功！')</script>");
		return "list";
	}

	public String toUpdatePwd(){
		return "updatePwd";
	}
	
	public String updatePwd(){
		try { 
			AdminUser adminUser = systemLogic.getAdminUserByName(name);
			String md5Password = CommonUtil.md5(prePassword);
			if(!md5Password.equals(adminUser.getPassword())){
				this.printHTML("<script>parent.callback(" + false + ", '"+ "原密码错误！"+"')</script>");
				return "index";
			}
			if(!password.equals(newPassword)){
				this.printHTML("<script>parent.callback(" + false + ", '"+ "两次新密码输入不一样！"+"')</script>");
				return "index";
			}
			adminUser.setPassword(password);
			systemService.updateAdminUser(adminUser);
		} catch (Exception e) {
			e.printStackTrace();
			this.printHTML("<script>parent.callback(" + false + ", '"+e.getMessage()+"')</script>");
			return "index";
		}
		this.printHTML("<script>parent.callback(" + true + ", '修改成功！')</script>");
		return "index";
	}
	
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getNewPassword() {
		return newPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

	public String getPrePassword() {
		return prePassword;
	}

	public void setPrePassword(String prePassword) {
		this.prePassword = prePassword;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public AdminUser getAdminUser() {
		return adminUser;
	}

	public void setAdminUser(AdminUser adminUser) {
		this.adminUser = adminUser;
	}
}
